package com.huiyujinrong.qiangdanbao.activity.login;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.utils.DataUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetActivity extends AppCompatActivity implements ForgetContract.View{


    @BindView(R.id.toolbar_Title)
    TextView title;
    @BindView(R.id.toolbar_Back)
    RelativeLayout back;
    @BindView(R.id.fg_pwd)
    EditText pwd;
    @BindView(R.id.fg_phone)
    EditText phone;
    @BindView(R.id.fg_code)
    EditText code;
    @BindView(R.id.fg_pwd_sh)
    ImageView pwd_sh;
    @BindView(R.id.fg_sendCode)
    TextView senCode;
    private boolean isShow = true;
    private ForgetActivity.TimeCount time;
    private ForgetContract.Presenter presenter;
    private int CODE_TYPE = 3;
    private Toast t;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        //短信倒计时
        time = new ForgetActivity.TimeCount(60000, 1000);
        inSetToolBar();

        presenter = new ForgetPresenter(this);
        presenter.start();

    }

    //设置toolbar
    private void inSetToolBar() {
        back.setVisibility(View.VISIBLE);
        title.setText("重置密码");
    }

    @OnClick(R.id.toolbar_Back)
    void onBack() {
        finish();
    }

    //  显示明文密码，隐藏密码
    @OnClick(R.id.fg_pwd_s_h)
    void setSH() {
        if (isShow) {
            pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            pwd_sh.setBackgroundResource(R.drawable.btn_zc_hide);
            isShow = false;
        } else {
            pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            pwd_sh.setBackgroundResource(R.drawable.btn_zc_show);
            isShow = true;
        }
        //切换后将EditText光标置于末尾
        CharSequence charSequence = pwd.getText();
        if (charSequence != null) {
            Selection.setSelection((Spannable) charSequence, charSequence.length());
        }
        //刷新界面
        pwd.postInvalidate();
    }


    //  发送验证码
    @OnClick(R.id.fg_sendCode)
    void inSendCode() {
        if(DataUtils.getCheckTel(phone.getText().toString())){
            presenter.sendCode(phone.getText().toString(), CODE_TYPE);
            time.start();
        }else {
            showToast("手机号有误");
        }
    }

    //修改密码
    @OnClick(R.id.rg_forget)
    void upDate() {
//        if (TextUtils.isEmpty(phone.getText().toString()) && phone.getText().length() < 11) {
//            showToast("请输入完整手机号");
//        } else {
//            if (TextUtils.isEmpty(code.getText().toString()) && code.getText().length() < 6) {
//                showToast("请输入完整验证码");
//            } else {
//                if (TextUtils.isEmpty(pwd.getText().toString()) && pwd.getText().length() < 6) {
//                    showToast("密码位数过少");
//                } else {
//                    presenter.update(phone.getText().toString(), code.getText().toString(), pwd.getText().toString());
//                }
//            }
//        }

        if(DataUtils.getCheckTel(phone.getText().toString())){
            if (TextUtils.isEmpty(code.getText().toString()) && code.getText().length() < 6) {
                showToast("验证码不正确");
            } else {
                if (TextUtils.isEmpty(pwd.getText().toString()) && pwd.getText().length() < 6) {
                    showToast("密码必须大于6位");
                } else {
                    presenter.update(phone.getText().toString(), code.getText().toString(), pwd.getText().toString());
                }
            }
        }else {
            showToast("手机号不正确");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.end();
    }

    @Override
    public void showToast(String str) {
        if(t == null) {
            t = Toast.makeText(this, str, Toast.LENGTH_SHORT);
        } else {
            t.setText(str);
            t.setDuration(Toast.LENGTH_SHORT);
        }
        t.show();
    }

    @Override
    public void exit() {
        finish();
    }

    //获取验证码  倒计时
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            senCode.setText(getString(R.string.sendCode));
//            senCode.setBackgroundResource(R.drawable.shape_login_showcode);
            senCode.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            senCode.setClickable(false);//防止重复点击
            senCode.setText(millisUntilFinished / 1000 + "s后重发");
        }
    }
}
